<?php

require_once 'FTPService.php';

/**
 * Description of generateRSS
 *
 * @author hodek
 */
function fibixCreateRSS() {
    $query = sprintf(Util::$select_validNews);    
    $result = mysql_query($query);
    $channel = new RSSChannel();
    $channel->description = Constants::$rssDesc;
    $channel->lastBuildDate = Util::getCurrentRSSTime();
    $channel->link = Constants::$fullLink;
    $channel->title = Constants::$mainHeader;
    $itemIndex = 0;
    while ($row1 = mysql_fetch_assoc($result)) {
        $items[$itemIndex] = new RSSitem();
        $items[$itemIndex]->description = $row1["present"];
        $items[$itemIndex]->pubDate = $row1["time"];
        $items[$itemIndex]->title = $row1["header"];
        $items[$itemIndex]->link = Constants::$fullLink;
        $itemIndex++;
    }
    saveRSSFTP($channel, $items);
    //generateRSS("form/upload", , $channel, $items);
}

function saveRSSFTP($rssChannel, $rssItemArray){
    $ftpService = connectFTP(Constants::$ftp_server, Constants::$ftp_login, Constants::$ftp_pass, Constants::$ftp_rss_dir);
    $ftpService->uploadFile(buildRSSString($rssChannel, $rssItemArray), "fibix.rss", "fibix.rss");
    $ftpService->closeFTP();
}

function buildRSSString($rssChannel,$rssItemArray){
    $stringData = '<?xml version="1.0" encoding="UTF-8" ?>';
    $stringData .= "\n";
    $stringData .= "\n";
    $stringData .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';    
    $stringData .= "\n";
    $stringData .= "<channel>\n";
    $stringData .= '<atom:link href="http://fibix.org/rss/fibix.rss"  rel="self" type="application/rss+xml" />';
    $stringData .= "\n";
    $stringData .= "\n";
    $stringData .= "\t<title>$rssChannel->title</title>\n";
    $stringData .= "\t<description>$rssChannel->description</description>\n";
    $stringData .= "\t<link>$rssChannel->link</link>\n";
    $stringData .= "\t<lastBuildDate>$rssChannel->lastBuildDate</lastBuildDate>\n";    
    $stringData .= "\n";
    foreach ($rssItemArray as $item) {
        $stringData .= "\t<item>\n";
        $stringData .= "\t\t<title>$item->title</title>\n";
        $stringData .= "\t\t<description>$item->description</description>\n";
        $stringData .= "\t\t<link>$item->link</link>\n";
        $stringData .= "\t\t<pubDate>".$item->convertPubDate()."</pubDate>\n";
        $stringData .= "\t</item>\n";
    }
    $stringData .= "\n";
    $stringData .= "</channel>";
    $stringData .= "\n";
    $stringData .= "</rss>";
    return $stringData;
}

function generateRSS($destinationFolder, $fileName, $rssChannel, $rssItemArray) {
    $ourFileHandle = fopen($destinationFolder+"/"+ $fileName, 'w') or die("can't open file $destinationFolder/$fileName"); 
    fwrite($ourFileHandle, buildRSSString($rssChannel, $rssItemArray));
    fclose($ourFileHandle);
}

function newLine($string) {
    $string .= "\n";
    return $string;
}

class RSSChannel {

    public $title;
    public $link;
    public $description;
    public $lastBuildDate;    

}

class RSSitem {

    public $title;
    public $link;
    public $description;
    public $pubDate;
    
    
    public function convertPubDate(){
        $timestamp = Util::getTimestampFromDBDateTime($this->pubDate);
        //echo "$timestamp->hour, (int)$timestamp->minute, (int)$timestamp->second, (int)$timestamp->month, (int)$timestamp->day, (int)$timestamp->year";
        $timestamp = mktime((int)$timestamp->hour, (int)$timestamp->minute, (int)$timestamp->second, (int)$timestamp->month, (int)$timestamp->day, (int)$timestamp->year);
        return date(DATE_RFC2822,$timestamp);        
    }

}

?>
